﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DALFactory;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class PurchaseOrderBLL
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private static readonly IPackDA packDA = DataAccess.CreatePackDA();
        private PubParasInfo pubParasInfo;
        private static readonly IPurchaseOrderDA purchaseOrderDA = DataAccess.CreatePurchaseOrderDA();
        private static readonly IPurchaseOrderDetailDA purchaseOrderDetailDA = DataAccess.CreatePurchaseOrderDetailDA();
        private static readonly ISkuDA skuDA = DataAccess.CreateSkuDA();

        public PurchaseOrderBLL()
        {
            this.pubParasInfo = null;
        }

        public PurchaseOrderBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public PurchaseOrderBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string ClosePurchaseOrder(List<string> poIDs)
        {
            string result = string.Empty;
            string whLoginID = this.dataBaseID[0];
            if ((poIDs != null) && (poIDs.Count > 0))
            {
                for (int i = 0; i < poIDs.Count; i++)
                {
                    DataBase dataBase = new DataBase();
                    DbConnection conn = dataBase.connection;
                    conn.Open();
                    DbTransaction tran = conn.BeginTransaction();
                    try
                    {
                        string status = PurchaseOrderStatus.Closed;
                        purchaseOrderDA.UpdatePurchaseOrderStatus(dataBase, tran, poIDs[i], status, whLoginID, this.pubParasInfo.UserID);
                        purchaseOrderDetailDA.UpdatePurchaseOrderDetailStatus(dataBase, tran, poIDs[i], status, whLoginID, this.pubParasInfo.UserID);
                        tran.Commit();
                        result = result + poIDs[i] + ",";
                    }
                    catch (Exception)
                    {
                        tran.Rollback();
                    }
                    finally
                    {
                        conn.Close();
                    }
                }
            }
            return (string.IsNullOrEmpty(result) ? "" : result.Substring(0, result.Length - 1));
        }

        public string DeletePurchaseOrder(List<string> poIDs)
        {
            string temp = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                List<PurchaseOrderInfo> purchaseOrderInfos = new List<PurchaseOrderInfo>();
                PurchaseOrderInfo purchaseOrderInfo = null;
                foreach (string id in poIDs)
                {
                    purchaseOrderInfo = purchaseOrderDA.GetPurchaseOrderByID(dataBase, tran, id, this.dataBaseID[0], this.pubParasInfo);
                    purchaseOrderInfo.PurchaseOrderDetailInfos = purchaseOrderDetailDA.GetPurchaseOrderDetailListByID(dataBase, tran, id, this.dataBaseID[0], this.pubParasInfo);
                    purchaseOrderInfos.Add(purchaseOrderInfo);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                purchaseOrderDetailDA.DeletePurchaseOrderDetail(dataBase, tran, poIDs, this.dataBaseID);
                if (purchaseOrderDA.DeletePurchaseOrder(dataBase, tran, poIDs, this.dataBaseID) > 0)
                {
                    for (int i = 0; i < poIDs.Count; i++)
                    {
                        auditDataBLL.InsertAuditData<PurchaseOrderInfo>(dataBase, tran, purchaseOrderInfos[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "PURCHASE_ORDER", this.dataBaseID[0]);
                        if ((purchaseOrderInfos[i].PurchaseOrderDetailInfos != null) && (purchaseOrderInfos[i].PurchaseOrderDetailInfos.Count > 0))
                        {
                            for (int j = 0; j < purchaseOrderInfos[i].PurchaseOrderDetailInfos.Count; j++)
                            {
                                auditDataBLL.InsertAuditData<PurchaseOrderDetailInfo>(dataBase, tran, purchaseOrderInfos[i].PurchaseOrderDetailInfos[j], null, AuditAction.Delete, this.pubParasInfo.UserID, "PURCHASE_ORDER_DETAIL", this.dataBaseID[0]);
                            }
                        }
                        temp = temp + poIDs[i] + ",";
                    }
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            if (temp.Length > 0)
            {
                return temp.Substring(0, temp.Length - 1);
            }
            return "";
        }

        public DataSet GetAllInfoPurchaseOrder()
        {
            return purchaseOrderDA.GetAllPurchaseOrder(this.dataBaseID[0], this.pubParasInfo);
        }

        public PurchaseOrderInfo GetPurchaseOrderInfoByID(string poID)
        {
            PurchaseOrderInfo purchaseOrder = purchaseOrderDA.GetPurchaseOrderByID(poID, this.dataBaseID[0], this.pubParasInfo);
            if (purchaseOrder != null)
            {
                purchaseOrder.PurchaseOrderDetailInfos = purchaseOrderDetailDA.GetPurchaseOrderDetailListByID(purchaseOrder.PoID, this.dataBaseID[0], this.pubParasInfo);
                if ((purchaseOrder.PurchaseOrderDetailInfos == null) || (purchaseOrder.PurchaseOrderDetailInfos.Count <= 0))
                {
                    return purchaseOrder;
                }
                InboundHelper inboundHelper = new InboundHelper();
                Dictionary<string, PackInfo> packInfoList = new Dictionary<string, PackInfo>();
                for (int i = 0; i < purchaseOrder.PurchaseOrderDetailInfos.Count; i++)
                {
                    PackInfo packInfo = null;
                    if (packInfoList.ContainsKey(purchaseOrder.PurchaseOrderDetailInfos[i].PackID))
                    {
                        packInfo = packInfoList[purchaseOrder.PurchaseOrderDetailInfos[i].PackID];
                    }
                    else
                    {
                        packInfo = packDA.GetPackByID(purchaseOrder.PurchaseOrderDetailInfos[i].PackID, this.dataBaseID[0]);
                        packInfoList.Add(purchaseOrder.PurchaseOrderDetailInfos[i].PackID, packInfo);
                    }
                    purchaseOrder.PurchaseOrderDetailInfos[i].OrderedQty = inboundHelper.ExchangeUom(packInfo, purchaseOrder.PurchaseOrderDetailInfos[i].UomID, purchaseOrder.PurchaseOrderDetailInfos[i].OrderedQty, "R");
                    purchaseOrder.PurchaseOrderDetailInfos[i].ReceivedQty = inboundHelper.ExchangeUom(packInfo, purchaseOrder.PurchaseOrderDetailInfos[i].UomID, purchaseOrder.PurchaseOrderDetailInfos[i].ReceivedQty, "R");
                }
            }
            return purchaseOrder;
        }

        public DataSet GetPurchaseOrderInfoByQueryEntity(PurchaseOrderQueryEntity purchaseOrderQueryEntity)
        {
            PurchaseOrderQueryEntity purchaseOrderQuery = purchaseOrderQueryEntity;
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(purchaseOrderQuery.PoID))
            {
                sqlWhere.Add(" PO.PO_ID " + purchaseOrderQuery.PoID);
            }
            if (!string.IsNullOrEmpty(purchaseOrderQuery.PoGroup))
            {
                sqlWhere.Add(" PO.PO_GROUP " + purchaseOrderQuery.PoGroup);
            }
            if (!string.IsNullOrEmpty(purchaseOrderQuery.Status))
            {
                sqlWhere.Add(" PO.STATUS " + purchaseOrderQuery.Status);
            }
            if (!string.IsNullOrEmpty(purchaseOrderQuery.OwnerID))
            {
                sqlWhere.Add(" PO.OWNER_ID " + purchaseOrderQuery.OwnerID);
            }
            if (!string.IsNullOrEmpty(purchaseOrderQuery.PoType))
            {
                sqlWhere.Add(" PO.PO_TYPE " + purchaseOrderQuery.PoType);
            }
            if (!string.IsNullOrEmpty(purchaseOrderQuery.PoDate))
            {
                sqlWhere.Add(purchaseOrderQuery.PoDate.Replace("@PoDate@", " PO.PO_DATE "));
            }
            if (!string.IsNullOrEmpty(purchaseOrderQuery.VendorID))
            {
                sqlWhere.Add(" PO.VENDOR_ID " + purchaseOrderQuery.VendorID);
            }
            if (!string.IsNullOrEmpty(purchaseOrderQuery.VendorName))
            {
                sqlWhere.Add(" PO.VENDOR_NAME " + purchaseOrderQuery.VendorName);
            }
            if (!string.IsNullOrEmpty(purchaseOrderQuery.VendorRef))
            {
                sqlWhere.Add(" PO.VENDOR_REF " + purchaseOrderQuery.VendorRef);
            }
            if (!string.IsNullOrEmpty(purchaseOrderQuery.IsActive))
            {
                sqlWhere.Add(" PO.IS_ACTIVE " + purchaseOrderQuery.IsActive);
            }
            if (!string.IsNullOrEmpty(purchaseOrderQuery.ExternalPoID))
            {
                sqlWhere.Add(" PO.EXTERNAL_PO_ID " + purchaseOrderQuery.ExternalPoID);
            }
            return purchaseOrderDA.GetPurchaseOrderByQueryList(sqlWhere, purchaseOrderQuery, this.dataBaseID[0], this.pubParasInfo);
        }

        public string InserOnLinePurchaseOrder(PurchaseOrderInfo purchaseOrderInfo)
        {
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            AuditDataBLL auditDataBLL = new AuditDataBLL();
            string poID = string.Empty;
            try
            {
                IOwnerDA ownerDA = DataAccess.CreateOwnerDA();
                if (!ownerDA.CheckTradePartnerIDUnique(purchaseOrderInfo.OwnerID, this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F3100_001", this.pubParasInfo.UserLanguage, new List<string> { purchaseOrderInfo.OwnerID });
                }
                else if (!commonMethod.CheckAuthorizationOwnerByUserID(this.pubParasInfo.UserID, purchaseOrderInfo.OwnerID))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F3100_002", this.pubParasInfo.UserLanguage, new List<string> { purchaseOrderInfo.OwnerID });
                }
                if (!string.IsNullOrEmpty(purchaseOrderInfo.VendorID))
                {
                    IVendorDA vendorDA = DataAccess.CreateVendorDA();
                    if (!vendorDA.CheckVendorIDUnique(dataBase, tran, purchaseOrderInfo.VendorID, this.dataBaseID[0]))
                    {
                        PublicMethod.GetInstance().ShowAlertMessage("F3100_003", this.pubParasInfo.UserLanguage, new List<string> { purchaseOrderInfo.VendorID });
                    }
                    else if (!vendorDA.CheckAuthorizationVendorIDUserID(dataBase, tran, purchaseOrderInfo.VendorID, this.dataBaseID[0], this.pubParasInfo))
                    {
                        PublicMethod.GetInstance().ShowAlertMessage("F2130_002", this.pubParasInfo.UserLanguage, new List<string> { purchaseOrderInfo.VendorID });
                    }
                }
                if (!string.IsNullOrEmpty(purchaseOrderInfo.PoID))
                {
                    if (purchaseOrderDA.CheckPurchaseOrderIDUnique(dataBase, tran, purchaseOrderInfo.PoID, this.dataBaseID[0]))
                    {
                        PublicMethod.GetInstance().ShowAlertMessage("F3100_004", this.pubParasInfo.UserLanguage, new List<string> { purchaseOrderInfo.PoID });
                    }
                    else
                    {
                        poID = purchaseOrderInfo.PoID;
                    }
                }
                else
                {
                    poID = commonMethod.GenNextNumStr(dataBase, tran, "PURCHASE_ORDER", this.dataBaseID[0]);
                }
                TradePartnerInfo owner = ownerDA.GetTradePartnerByID(dataBase, tran, purchaseOrderInfo.OwnerID, this.dataBaseID[0]);
                if ((owner != null) && !string.IsNullOrEmpty(owner.OwnerID))
                {
                    if (!string.IsNullOrEmpty(owner.PoAlphaPrefix))
                    {
                        poID = owner.PoAlphaPrefix + poID;
                    }
                }
                else
                {
                    List<string> gInitLocal10 = new List<string> {
                        purchaseOrderInfo.OwnerID
                    };
                    PublicMethod.GetInstance().ShowAlertMessage("F3100_005", this.pubParasInfo.UserLanguage, gInitLocal10);
                }
                purchaseOrderInfo.PoID = poID;
                PurchaseOrderInfo purchaseOrder = purchaseOrderInfo;
                purchaseOrder.UpdatedBy = purchaseOrder.CreatedBy;
                purchaseOrder.CreatedDate = DateTime.Now;
                purchaseOrder.UpdatedDate = DateTime.Now;
                purchaseOrderDA.InsertPurchaseOrder(dataBase, tran, purchaseOrder, this.dataBaseID);
                auditDataBLL.InsertAuditData<PurchaseOrderInfo>(dataBase, tran, null, purchaseOrder, AuditAction.Insert, this.pubParasInfo.UserID, "PURCHASE_ORDER", this.dataBaseID[0]);
                poID = purchaseOrder.PoID;
                if ((purchaseOrder.PurchaseOrderDetailInfos != null) && (purchaseOrder.PurchaseOrderDetailInfos.Count > 0))
                {
                    for (int i = 0; i < purchaseOrder.PurchaseOrderDetailInfos.Count; i++)
                    {
                        purchaseOrder.PurchaseOrderDetailInfos[i].PoID = poID;
                        if (purchaseOrderDetailDA.CheckPurchaseOrderDetailIDUnique(dataBase, tran, purchaseOrder.PurchaseOrderDetailInfos[i].PoID, purchaseOrder.PurchaseOrderDetailInfos[i].LineID.ToString(), this.dataBaseID[0]))
                        {
                            PublicMethod.GetInstance().ShowAlertMessage("F3100_006", this.pubParasInfo.UserLanguage, new List<string> { purchaseOrder.PurchaseOrderDetailInfos[i].PoID, purchaseOrder.PurchaseOrderDetailInfos[i].LineID.ToString() });
                        }
                        else
                        {
                            SkuInfo skuinfo = skuDA.GetSkuByID(dataBase, tran, purchaseOrderInfo.PurchaseOrderDetailInfos[i].OwnerID, purchaseOrderInfo.PurchaseOrderDetailInfos[i].SkuID, this.dataBaseID[0]);
                            if (((skuinfo == null) && string.IsNullOrEmpty(skuinfo.SkuID)) && (skuinfo.IsActive == "Y"))
                            {
                                PublicMethod.GetInstance().ShowAlertMessage("F7100_001", this.pubParasInfo.UserLanguage, new List<string> { purchaseOrderInfo.PurchaseOrderDetailInfos[i].SkuID });
                            }
                            purchaseOrder.PurchaseOrderDetailInfos[i].UnitPrice = Convert.ToDouble(skuinfo.NetPrice);
                            purchaseOrder.PurchaseOrderDetailInfos[i].TotalCube = Convert.ToDouble(skuinfo.Cube) * Convert.ToDouble(purchaseOrder.PurchaseOrderDetailInfos[i].OrderedQty);
                            purchaseOrder.PurchaseOrderDetailInfos[i].TotalGrossWeight = Convert.ToDouble(skuinfo.GrossWgt) * Convert.ToDouble(purchaseOrder.PurchaseOrderDetailInfos[i].OrderedQty);
                            purchaseOrder.PurchaseOrderDetailInfos[i].TotalNetWeight = Convert.ToDouble(skuinfo.NetWgt) * Convert.ToDouble(purchaseOrder.PurchaseOrderDetailInfos[i].OrderedQty);
                            purchaseOrder.PurchaseOrderDetailInfos[i].TotalPrice = Convert.ToDouble(skuinfo.NetPrice) * Convert.ToDouble(purchaseOrder.PurchaseOrderDetailInfos[i].OrderedQty);
                            purchaseOrder.PurchaseOrderDetailInfos[i].Status = "New";
                            purchaseOrder.PurchaseOrderDetailInfos[i].ReceivedQty = 0.0M;
                            purchaseOrder.PurchaseOrderDetailInfos[i].RejectedQty = 0.0M;
                            purchaseOrder.PurchaseOrderDetailInfos[i].CreatedDate = DateTime.Now;
                            purchaseOrder.PurchaseOrderDetailInfos[i].UpdatedDate = DateTime.Now;
                            purchaseOrder.PurchaseOrderDetailInfos[i].CreatedBy = this.pubParasInfo.UserID;
                            purchaseOrder.PurchaseOrderDetailInfos[i].UpdatedBy = this.pubParasInfo.UserID;
                            purchaseOrderDetailDA.InsertPurchaseOrderDetail(dataBase, tran, purchaseOrder.PurchaseOrderDetailInfos[i], this.dataBaseID);
                            auditDataBLL.InsertAuditData<PurchaseOrderDetailInfo>(dataBase, tran, null, purchaseOrder.PurchaseOrderDetailInfos[i], AuditAction.Insert, this.pubParasInfo.UserID, "PURCHASE_ORDER_DETAIL", this.dataBaseID[0]);
                        }
                    }
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return poID;
        }

        public string InserPurchaseOrder(PurchaseOrderInfo purchaseOrderInfo)
        {
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            AuditDataBLL auditDataBLL = new AuditDataBLL();
            string poID = string.Empty;
            try
            {
                IOwnerDA ownerDA = DataAccess.CreateOwnerDA();
                if (!ownerDA.CheckTradePartnerIDUnique(purchaseOrderInfo.OwnerID, this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F3100_001", this.pubParasInfo.UserLanguage, new List<string> { purchaseOrderInfo.OwnerID });
                }
                else if (!commonMethod.CheckAuthorizationOwnerByUserID(this.pubParasInfo.UserID, purchaseOrderInfo.OwnerID))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F3100_002", this.pubParasInfo.UserLanguage, new List<string> { purchaseOrderInfo.OwnerID });
                }
                if (!string.IsNullOrEmpty(purchaseOrderInfo.VendorID))
                {
                    IVendorDA vendorDA = DataAccess.CreateVendorDA();
                    if (!vendorDA.CheckVendorIDUnique(dataBase, tran, purchaseOrderInfo.VendorID, this.dataBaseID[0]))
                    {
                        PublicMethod.GetInstance().ShowAlertMessage("F3100_003", this.pubParasInfo.UserLanguage, new List<string> { purchaseOrderInfo.VendorID });
                    }
                    else if (!vendorDA.CheckAuthorizationVendorIDUserID(dataBase, tran, purchaseOrderInfo.VendorID, this.dataBaseID[0], this.pubParasInfo))
                    {
                        PublicMethod.GetInstance().ShowAlertMessage("F2130_002", this.pubParasInfo.UserLanguage, new List<string> { purchaseOrderInfo.VendorID });
                    }
                }
                if (!string.IsNullOrEmpty(purchaseOrderInfo.PoID))
                {
                    if (purchaseOrderDA.CheckPurchaseOrderIDUnique(dataBase, tran, purchaseOrderInfo.PoID, this.dataBaseID[0]))
                    {
                        PublicMethod.GetInstance().ShowAlertMessage("F3100_004", this.pubParasInfo.UserLanguage, new List<string> { purchaseOrderInfo.PoID });
                    }
                    else
                    {
                        poID = purchaseOrderInfo.PoID;
                    }
                }
                else
                {
                    poID = commonMethod.GenNextNumStr(dataBase, tran, "PURCHASE_ORDER", this.dataBaseID[0]);
                }
                TradePartnerInfo owner = ownerDA.GetTradePartnerByID(dataBase, tran, purchaseOrderInfo.OwnerID, this.dataBaseID[0]);
                if ((owner != null) && !string.IsNullOrEmpty(owner.OwnerID))
                {
                    if (!string.IsNullOrEmpty(owner.PoAlphaPrefix))
                    {
                        poID = owner.PoAlphaPrefix + poID;
                    }
                }
                else
                {
                    List<string> gInitLocal5 = new List<string> {
                        purchaseOrderInfo.OwnerID
                    };
                    PublicMethod.GetInstance().ShowAlertMessage("F3100_005", this.pubParasInfo.UserLanguage, gInitLocal5);
                }
                purchaseOrderInfo.PoID = poID;
                PurchaseOrderInfo purchaseOrder = purchaseOrderInfo;
                purchaseOrder.UpdatedBy = purchaseOrder.CreatedBy;
                purchaseOrder.CreatedDate = DateTime.Now;
                purchaseOrder.UpdatedDate = DateTime.Now;
                purchaseOrderDA.InsertPurchaseOrder(dataBase, tran, purchaseOrder, this.dataBaseID);
                auditDataBLL.InsertAuditData<PurchaseOrderInfo>(dataBase, tran, null, purchaseOrder, AuditAction.Insert, this.pubParasInfo.UserID, "PURCHASE_ORDER", this.dataBaseID[0]);
                poID = purchaseOrder.PoID;
                if ((purchaseOrder.PurchaseOrderDetailInfos != null) && (purchaseOrder.PurchaseOrderDetailInfos.Count > 0))
                {
                    for (int i = 0; i < purchaseOrder.PurchaseOrderDetailInfos.Count; i++)
                    {
                        purchaseOrder.PurchaseOrderDetailInfos[i].PoID = poID;
                        if (purchaseOrderDetailDA.CheckPurchaseOrderDetailIDUnique(dataBase, tran, purchaseOrder.PurchaseOrderDetailInfos[i].PoID, purchaseOrder.PurchaseOrderDetailInfos[i].LineID.ToString(), this.dataBaseID[0]))
                        {
                            PublicMethod.GetInstance().ShowAlertMessage("F3100_006", this.pubParasInfo.UserLanguage, new List<string> { purchaseOrder.PurchaseOrderDetailInfos[i].PoID, purchaseOrder.PurchaseOrderDetailInfos[i].LineID.ToString() });
                        }
                        else
                        {
                            purchaseOrder.PurchaseOrderDetailInfos[i].Status = "New";
                            purchaseOrder.PurchaseOrderDetailInfos[i].ReceivedQty = 0.0M;
                            purchaseOrder.PurchaseOrderDetailInfos[i].RejectedQty = 0.0M;
                            purchaseOrder.PurchaseOrderDetailInfos[i].CreatedDate = DateTime.Now;
                            purchaseOrder.PurchaseOrderDetailInfos[i].UpdatedDate = DateTime.Now;
                            purchaseOrder.PurchaseOrderDetailInfos[i].CreatedBy = this.pubParasInfo.UserID;
                            purchaseOrder.PurchaseOrderDetailInfos[i].UpdatedBy = this.pubParasInfo.UserID;
                            purchaseOrderDetailDA.InsertPurchaseOrderDetail(dataBase, tran, purchaseOrder.PurchaseOrderDetailInfos[i], this.dataBaseID);
                            auditDataBLL.InsertAuditData<PurchaseOrderDetailInfo>(dataBase, tran, null, purchaseOrder.PurchaseOrderDetailInfos[i], AuditAction.Insert, this.pubParasInfo.UserID, "PURCHASE_ORDER_DETAIL", this.dataBaseID[0]);
                        }
                    }
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return poID;
        }

        public string UpdateOnLinePurchaseOrder(PurchaseOrderInfo purchaseOrderInfo)
        {
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string poID = string.Empty;
            try
            {
                if (!DataAccess.CreateOwnerDA().CheckTradePartnerIDUnique(purchaseOrderInfo.OwnerID, this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F3100_001", this.pubParasInfo.UserLanguage, new List<string> { purchaseOrderInfo.OwnerID });
                }
                else if (!commonMethod.CheckAuthorizationOwnerByUserID(this.pubParasInfo.UserID, purchaseOrderInfo.OwnerID))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F3100_002", this.pubParasInfo.UserLanguage, new List<string> { purchaseOrderInfo.OwnerID });
                }
                if (!string.IsNullOrEmpty(purchaseOrderInfo.VendorID))
                {
                    IVendorDA vendorDA = DataAccess.CreateVendorDA();
                    if (!vendorDA.CheckVendorIDUnique(purchaseOrderInfo.VendorID, this.dataBaseID[0]))
                    {
                        PublicMethod.GetInstance().ShowAlertMessage("F3100_003", this.pubParasInfo.UserLanguage, new List<string> { purchaseOrderInfo.VendorID });
                    }
                    else if (!vendorDA.CheckAuthorizationVendorIDUserID(purchaseOrderInfo.VendorID, this.dataBaseID[0], this.pubParasInfo))
                    {
                        PublicMethod.GetInstance().ShowAlertMessage("F2130_002", this.pubParasInfo.UserLanguage, new List<string> { purchaseOrderInfo.VendorID });
                    }
                }
                PurchaseOrderInfo oldPurchaseOrderInfo = purchaseOrderDA.GetPurchaseOrderByID(purchaseOrderInfo.PoID, this.dataBaseID[0], this.pubParasInfo);
                if (purchaseOrderDA.UpdatePurchaseOrder(dataBase, tran, purchaseOrderInfo, this.dataBaseID) > 0)
                {
                    AuditDataBLL auditDataBLL = new AuditDataBLL();
                    auditDataBLL.InsertAuditData<PurchaseOrderInfo>(oldPurchaseOrderInfo, purchaseOrderInfo, AuditAction.Update, this.pubParasInfo.UserID, "PURCHASE_ORDER", this.dataBaseID[0]);
                    poID = oldPurchaseOrderInfo.PoID;
                    if ((purchaseOrderInfo.PurchaseOrderDetailInfos != null) && (purchaseOrderInfo.PurchaseOrderDetailInfos.Count > 0))
                    {
                        int i;
                        if ((oldPurchaseOrderInfo.PurchaseOrderDetailInfos != null) && (oldPurchaseOrderInfo.PurchaseOrderDetailInfos.Count > 0))
                        {
                            for (i = 0; i < oldPurchaseOrderInfo.PurchaseOrderDetailInfos.Count; i++)
                            {
                                auditDataBLL.InsertAuditData<PurchaseOrderDetailInfo>(dataBase, tran, oldPurchaseOrderInfo.PurchaseOrderDetailInfos[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "PURCHASE_ORDER_DETAIL", this.dataBaseID[0]);
                            }
                        }
                        List<string> gInitLocal17 = new List<string> {
                            poID
                        };
                        purchaseOrderDetailDA.DeletePurchaseOrderDetail(dataBase, tran, gInitLocal17, this.dataBaseID);
                        for (i = 0; i < purchaseOrderInfo.PurchaseOrderDetailInfos.Count; i++)
                        {
                            SkuInfo skuinfo = skuDA.GetSkuByID(dataBase, tran, purchaseOrderInfo.PurchaseOrderDetailInfos[i].OwnerID, purchaseOrderInfo.PurchaseOrderDetailInfos[i].SkuID, this.dataBaseID[0]);
                            if (((skuinfo == null) && string.IsNullOrEmpty(skuinfo.SkuID)) && (skuinfo.IsActive == "Y"))
                            {
                                PublicMethod.GetInstance().ShowAlertMessage("F7100_001", this.pubParasInfo.UserLanguage, new List<string> { purchaseOrderInfo.PurchaseOrderDetailInfos[i].SkuID });
                            }
                            purchaseOrderInfo.PurchaseOrderDetailInfos[i].PoID = poID;
                            purchaseOrderInfo.PurchaseOrderDetailInfos[i].LineID = (i + 1) * 10;
                            purchaseOrderInfo.PurchaseOrderDetailInfos[i].CreatedBy = this.pubParasInfo.UserID;
                            purchaseOrderInfo.PurchaseOrderDetailInfos[i].CreatedDate = DateTime.Now;
                            purchaseOrderInfo.PurchaseOrderDetailInfos[i].UpdatedBy = this.pubParasInfo.UserID;
                            purchaseOrderInfo.PurchaseOrderDetailInfos[i].UpdatedDate = DateTime.Now;
                            purchaseOrderInfo.PurchaseOrderDetailInfos[i].UnitPrice = Convert.ToDouble(skuinfo.NetPrice);
                            purchaseOrderInfo.PurchaseOrderDetailInfos[i].TotalCube = Convert.ToDouble(skuinfo.Cube) * Convert.ToDouble(purchaseOrderInfo.PurchaseOrderDetailInfos[i].OrderedQty);
                            purchaseOrderInfo.PurchaseOrderDetailInfos[i].TotalGrossWeight = Convert.ToDouble(skuinfo.GrossWgt) * Convert.ToDouble(purchaseOrderInfo.PurchaseOrderDetailInfos[i].OrderedQty);
                            purchaseOrderInfo.PurchaseOrderDetailInfos[i].TotalNetWeight = Convert.ToDouble(skuinfo.NetWgt) * Convert.ToDouble(purchaseOrderInfo.PurchaseOrderDetailInfos[i].OrderedQty);
                            purchaseOrderInfo.PurchaseOrderDetailInfos[i].TotalPrice = Convert.ToDouble(skuinfo.NetPrice) * Convert.ToDouble(purchaseOrderInfo.PurchaseOrderDetailInfos[i].OrderedQty);
                            purchaseOrderDetailDA.InsertPurchaseOrderDetail(dataBase, tran, purchaseOrderInfo.PurchaseOrderDetailInfos[i], this.dataBaseID);
                            auditDataBLL.InsertAuditData<PurchaseOrderDetailInfo>(dataBase, tran, null, purchaseOrderInfo.PurchaseOrderDetailInfos[i], AuditAction.Insert, this.pubParasInfo.UserID, "PURCHASE_ORDER_DETAIL", this.dataBaseID[0]);
                        }
                        poID = purchaseOrderInfo.PoID;
                    }
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return poID;
        }

        public string UpdatePurchaseOrder(PurchaseOrderInfo purchaseOrderInfo)
        {
            string poID = string.Empty;
            try
            {
                if (!DataAccess.CreateOwnerDA().CheckTradePartnerIDUnique(purchaseOrderInfo.OwnerID, this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F3100_001", this.pubParasInfo.UserLanguage, new List<string> { purchaseOrderInfo.OwnerID });
                }
                else if (!commonMethod.CheckAuthorizationOwnerByUserID(this.pubParasInfo.UserID, purchaseOrderInfo.OwnerID))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F3100_002", this.pubParasInfo.UserLanguage, new List<string> { purchaseOrderInfo.OwnerID });
                }
                if (!string.IsNullOrEmpty(purchaseOrderInfo.VendorID))
                {
                    IVendorDA vendorDA = DataAccess.CreateVendorDA();
                    if (!vendorDA.CheckVendorIDUnique(purchaseOrderInfo.VendorID, this.dataBaseID[0]))
                    {
                        PublicMethod.GetInstance().ShowAlertMessage("F3100_003", this.pubParasInfo.UserLanguage, new List<string> { purchaseOrderInfo.VendorID });
                    }
                    else if (!vendorDA.CheckAuthorizationVendorIDUserID(purchaseOrderInfo.VendorID, this.dataBaseID[0], this.pubParasInfo))
                    {
                        PublicMethod.GetInstance().ShowAlertMessage("F2130_002", this.pubParasInfo.UserLanguage, new List<string> { purchaseOrderInfo.VendorID });
                    }
                }
                PurchaseOrderInfo oldPurchaseOrderInfo = purchaseOrderDA.GetPurchaseOrderByID(purchaseOrderInfo.PoID, this.dataBaseID[0], this.pubParasInfo);
                if (purchaseOrderDA.UpdatePurchaseOrder(purchaseOrderInfo, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<PurchaseOrderInfo>(oldPurchaseOrderInfo, purchaseOrderInfo, AuditAction.Update, this.pubParasInfo.UserID, "PURCHASE_ORDER", this.dataBaseID[0]);
                    poID = purchaseOrderInfo.PoID;
                }
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return poID;
        }
    }
}

